package com.flow.model;

import java.util.List;

@SuppressWarnings("all")
public class PagerModel {

	/*
	 * 起始位置
	 */
	private static ThreadLocal offset = new ThreadLocal();
	/*
	 * 每页几行
	 */
	private static ThreadLocal pagesize = new ThreadLocal();

	/*
	 * 总记录数
	 */
	private int total;

	/*
	 * 当前页结果集
	 */
	private List datas;

	public List getDatas() {
		return datas;
	}

	public void setDatas(List datas) {
		this.datas = datas;
	}

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}

	public static int getOffset() {
		Integer os = (Integer) offset.get();
		if (os == null) {
			return 0;
		}
		return os;
	}

	public static void setOffset(int offsetvalue) {
		offset.set(offsetvalue);
	}

	public static void removeOffset() {
		offset.remove();
	}

	public static int getPagesize() {
		Integer ps = (Integer) pagesize.get();
		if (ps == null) {
			return Integer.MAX_VALUE;
		}
		return ps;
	}

	public static void setPagesize(int pagesizevalue) {
		pagesize.set(pagesizevalue);
	}

	public static void removePagesize() {
		pagesize.remove();
	}

	public String getTotalCountHql(String hql, List<Object> params) {

		String countHql = hql;
		int index = hql.indexOf("from");
		int order = hql.indexOf("order by");
		int group = hql.indexOf("group by");
		if (index != -1) {
			if (group > 0) {
				countHql = "select count(*) " + hql.substring(index, group);
			} else if (order > 0) {
				countHql = "select count(*) " + hql.substring(index, order);
			}
			countHql = "select count(*) " + hql.substring(index);
		} else {
			throw new RuntimeException("无效的HQL查询语句");
		}
		return countHql;
	}

}
